include pkg/pkg.mk

# 平台选择(全部注释则使用系统gcc)
# include plat-imx6ull.mk
# include plat-ls3a4000.mk
# include plat-ls3a5000.mk
# include plat-ohos.mk
# include plat-hmos.mk

# 基础路径
ROOT = $(shell pwd)
LIBS_PATH = $(ROOT)/libs
PKG_PATH = $(ROOT)/pkg
INSTALL_PATH = $(ROOT)/install

# 交叉编译器配置
# 在前面 plat-xxx.mk 中进行设置可以灵活覆盖掉以下配置
ARCH ?= $(shell uname -m)
GCC ?= $(CROSS)gcc
GXX ?= $(CROSS)g++
AR ?= $(CROSS)ar
AS ?= $(CROSS)as
NM ?= $(CROSS)nm
RANLIB ?= $(CROSS)ranlib
STRIP ?= $(CROSS)strip
OBJDUMP ?= $(CROSS)objdump
LD ?= $(CROSS)ld

# ---------- 基本方法 ----------

help:
	@echo "Help:" && \
	echo "  make x264    ## build single lib" && \
	echo "  make all     ## build all libs" && \
	echo "  make install ## install to: $(INSTALL_PATH)" && \
	echo "" && \
	echo "Check Current Config:" && \
	echo "  ARCH: $(ARCH)" && \
	echo "  CROSS: $(CROSS)" && \
	echo "  GCC: $(GCC)" && \
	echo "  GXX: $(GXX)" && \
	echo "  AR: $(AR)" && \
	echo "  AS: $(AS)" && \
	echo "  NM: $(NM)" && \
	echo "  RANLIB: $(RANLIB)" && \
	echo "  STRIP: $(STRIP)" && \
	echo "  OBJDUMP: $(OBJDUMP)" && \
	echo "  LD: $(LD)" && \
	echo "  EXT_CFLAGS: $(EXT_CFLAGS)" && \
	echo "  EXT_INCS: $(EXT_INCS)" && \
	echo "  EXT_LIBS: $(EXT_LIBS)" && \
	echo ""

install_static:
	@mkdir -p $(INSTALL_PATH)_static/lib && \
	cp -r $(LIBS_PATH)/include $(INSTALL_PATH)_static/ && \
	cp -r $(LIBS_PATH)/lib/*.a $(INSTALL_PATH)_static/lib/ && \
	unlink $(INSTALL_PATH)_static/lib/libpng.a

# 打包全部.a文件到 libffmpeg.so
install_shared: install_static
	@mkdir -p $(INSTALL_PATH)_shared/lib && \
	cp -r $(LIBS_PATH)/include $(INSTALL_PATH)_shared/ && \
	$(GCC) -s -fPIC -shared -Xlinker -zmuldefs -o $(INSTALL_PATH)_shared/lib/libffmpeg.so \
		$(EXT_CFLAGS) \
		-Wl,--whole-archive \
		${wildcard $(INSTALL_PATH)_static/lib/*.a} \
		-Wl,--no-whole-archive \
		-lpthread -ldl -lm && \
	cp $(LIBS_PATH)/lib/libavcodec.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libavfilter.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libavformat.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libavutil.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libswresample.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libswscale.so* $(INSTALL_PATH)_shared/lib/ && \
	cp $(LIBS_PATH)/lib/libpostproc.so* $(INSTALL_PATH)_shared/lib/

install:
	@mkdir -p $(INSTALL_PATH) && \
	cp -r $(LIBS_PATH)/include $(INSTALL_PATH)/ && \
	cp -r $(LIBS_PATH)/lib $(INSTALL_PATH)/ && \
	rm -rf $(INSTALL_PATH)/lib/*.la $(INSTALL_PATH)/lib/pkgconfig $(INSTALL_PATH)/lib/*.sh

clean:
	@rm -rf $(ROOT)/sample/*.out
cleanall: clean
	@rm -rf $(LIBS_PATH)/* ./install*

# ---------- 测试程序编译 ----------

TEST_SRC += ./sample/ffmpeg_codec.c
TEST_CFLAGS += -I./sample
ifeq ($(BYSHARED),1)
TEST_CFLAGS += -I$(INSTALL_PATH)_shared/include
TEST_CFLAGS += -L$(INSTALL_PATH)_shared/lib
TEST_CFLAGS += -lffmpeg -lpthread -lm -ldl -Wall
else
TEST_CFLAGS += -I$(LIBS_PATH)/include
TEST_CFLAGS += -L$(LIBS_PATH)/lib
TEST_CFLAGS += -lavfilter -lavformat -lavcodec -lswscale -lavutil -lavresample -lswresample -lpostproc
TEST_CFLAGS += -lfontconfig
TEST_CFLAGS += -lxml2
TEST_CFLAGS += -lvo-amrwbenc
TEST_CFLAGS += -lrtmp
TEST_CFLAGS += -lx264
TEST_CFLAGS += -lvpx
TEST_CFLAGS += -lmp3lame
TEST_CFLAGS += -lcurl
TEST_CFLAGS += -lcrypto -lssl
TEST_CFLAGS += -lfreetype
TEST_CFLAGS += -lpng12
TEST_CFLAGS += -lz
TEST_CFLAGS += -lpthread -lm -ldl
TEST_CFLAGS += -Wall -static #静态编译
endif
TEST_CFLAGS += $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)
test:
	@$(GCC) -o ./sample/test_encode_h264.out ./sample/test_encode_h264.c $(TEST_SRC) $(TEST_CFLAGS)
	@$(GCC) -o ./sample/test_decode_h264.out ./sample/test_decode_h264.c $(TEST_SRC) $(TEST_CFLAGS)

# ---------- 编译配置 ----------

# 注意依赖顺序
all: zlib png freetype openssl curl lame vpx x264 rtmp vo-amrwbenc xml2 fontconfig ffmpeg
	@echo "---------- make all complete ----------"

# 生成.so时 --enable-shared 反则 --disable-shared
ffmpeg:
	$(ffmpeg-dpkg) $(PKG_PATH)/$(ffmpeg-name).$(ffmpeg-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(ffmpeg-name) && \
	export PKG_CONFIG_PATH=$(LIBS_PATH)/lib/pkgconfig && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--arch=$(ARCH) \
		--cross-prefix=$(CROSS) \
		--enable-cross-compile \
		--target-os=linux \
		--disable-asm \
		--disable-programs \
		--disable-avdevice \
		--enable-shared \
		--enable-bsf=h264_mp4toannexb \
		--enable-bsf=aac_adtstoasc \
		--enable-small \
		--enable-gpl \
		--enable-version3 \
		--enable-nonfree \
		--enable-avresample \
		--enable-encoder=libx264 \
		--enable-encoder=libvpx_vp8 \
		--enable-encoder=aac \
		--enable-encoder=wmv2 \
		--enable-encoder=wmav2 \
		--enable-encoder=mjpeg \
		--enable-encoder=png \
		--enable-encoder=libmp3lame \
		--enable-encoder=libvo_amrwbenc \
		--enable-encoder=adpcm_swf \
		--enable-encoder=adpcm_ima_wav \
		--enable-encoder=pcm_s16le \
		--enable-encoder=pcm_mulaw \
		--enable-encoder=pcm_alaw \
		--enable-decoder=amrwb \
		--enable-decoder=h264 \
		--enable-decoder=libvpx_vp8 \
		--enable-decoder=aac \
		--enable-decoder=flv \
		--enable-decoder=mp3 \
		--enable-decoder=wmv3 \
		--enable-decoder=wmav2 \
		--enable-decoder=mjpeg \
		--enable-decoder=gif \
		--enable-decoder=png \
		--enable-decoder=pcm_s16le \
		--enable-decoder=pcm_mulaw \
		--enable-decoder=pcm_alaw \
		--enable-decoder=adpcm_swf \
		--enable-decoder=adpcm_ima_wav \
		--enable-muxer=mov \
		--enable-muxer=mp4 \
		--enable-muxer=asf \
		--enable-muxer=flv \
		--enable-muxer=h264 \
		--enable-muxer=m4v \
		--enable-muxer=mjpeg \
		--enable-muxer=amr \
		--enable-muxer=image2 \
		--enable-muxer=mp3 \
		--enable-muxer=wav \
		--enable-muxer=pcm_mulaw \
		--enable-muxer=mxf \
		--enable-muxer=vc1 \
		--enable-muxer=dnxhd \
		--enable-muxer=webm \
		--enable-muxer=rtsp \
		--enable-muxer=opus \
		--enable-demuxer=amr \
		--enable-demuxer=asf \
		--enable-demuxer=mp3 \
		--enable-demuxer=mov \
		--enable-demuxer=h264 \
		--enable-demuxer=aac \
		--enable-demuxer=flv \
		--enable-demuxer=live_flv \
		--enable-demuxer=wav \
		--enable-demuxer=gif \
		--enable-demuxer=mjpeg \
		--enable-demuxer=mpjpeg \
		--enable-demuxer=image_bmp_pipe \
		--enable-demuxer=image_jpeg_pipe \
		--enable-demuxer=image_jpegls_pipe \
		--enable-demuxer=image_png_pipe \
		--enable-demuxer=webm_dash_manifest \
		--enable-demuxer=dnxhd \
		--enable-demuxer=matroska,webm \
		--enable-demuxer=vc1 \
		--enable-demuxer=rtsp \
		--enable-parser=h264 \
		--enable-parser=aac \
		--enable-parser=mpeg4video \
		--enable-parser=mjpeg \
		--enable-parser=png \
		--enable-parser=bmp \
		--enable-parser=vp8 \
		--enable-avfilter \
		--enable-filter=overlay \
		--enable-filter=scale \
		--enable-filter=movie \
		--enable-filter=colorchannelmixer \
		--enable-filter=drawtext \
		--enable-filter=eq \
		--enable-filter=format \
		--enable-filter=amix \
		--enable-filter=aresample \
		--enable-filter=brchromakey \
		--enable-pic \
		--enable-zlib \
		--enable-libx264 \
		--enable-libfreetype \
		--enable-libvpx \
		--enable-libmp3lame \
		--enable-librtmp \
		--enable-libvo-amrwbenc \
		--enable-libfontconfig \
		--extra-libs="" \
		--pkg-config=pkg-config \
		--pkg-config-flags="--static" \
		--cc=$(GCC) \
		--strip=$(STRIP) \
		--extra-cflags="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -I$(LIBS_PATH)/include/freetype2 -L$(LIBS_PATH)/lib" \
		--extra-ldflags="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" && \
	sed -i 's%feclearexcept(%//feclearexcept(%' ./libavfilter/vf_drawtext.c && \
	sed -i 's%fetestexcept(%0)){//fetestexcept(%' ./libavfilter/vf_drawtext.c && \
	sed -i 's%SLIB_INSTALL_NAME=$$(SLIBNAME_WITH_VERSION)%SLIB_INSTALL_NAME=$$(SLIBNAME)%g' ./config.mak && \
	sed -i 's%SLIB_INSTALL_LINKS=$$(SLIBNAME_WITH_MAJOR) $$(SLIBNAME)%SLIB_INSTALL_LINKS=%g' ./config.mak && \
	sed -i 's%SLIBNAME_WITH_VERSION=$$(SLIBNAME).$$(LIBVERSION)%SLIBNAME_WITH_VERSION=$$(SLIBNAME)%g' ./config.mak && \
	sed -i 's%SLIBNAME_WITH_MAJOR=$$(SLIBNAME).$$(LIBMAJOR)%SLIBNAME_WITH_MAJOR=$$(SLIBNAME)%g' ./config.mak && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(ffmpeg-name)

#  二选一: --enable-libxml2  --enable-libexpat
fontconfig:
	$(fontconfig-dpkg) $(PKG_PATH)/$(fontconfig-name).$(fontconfig-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(fontconfig-name) && \
	export PKG_CONFIG_PATH=$(LIBS_PATH)/lib/pkgconfig && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-shared=no \
		--enable-libxml2 \
		--with-pic \
		--disable-docs \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		OBJDUMP=$(OBJDUMP) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -D__linux__ -I$(LIBS_PATH)/include/freetype2" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	patch -p1 < $(PKG_PATH)/$(fontconfig-name).patch && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(fontconfig-name)

# 使用GCC暂时编译不过
# make buildlib && make buildlib
expat:
	$(expat-dpkg) $(PKG_PATH)/$(expat-name).$(expat-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(expat-name) && \
	sed -i '/^#undef \x24\x32/a #endif' ./configure && \
	sed -i '/^#undef \x24\x32/a void __aeabi_unwind_cpp_pr0() {}' ./configure && \
	sed -i '/^#undef \x24\x32/a #ifndef __aeabi_unwind_cpp_pr0' ./configure && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=arm \
		--enable-static \
		--with-pic \
		CC=$(GCC) \
		CXX=$(GXX) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		CXXFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(expat-name)

xml2:
	$(xml2-dpkg) $(PKG_PATH)/$(xml2-name).$(xml2-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(xml2-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=arm \
		--enable-static \
		--with-python=no \
		--with-zlib=no \
		--with-sax1 \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	sed -i 's%format =%formatLocation =%' ./xmllint.c && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(xml2-name)

# 生成.so时 --enable-shared 反则 --enable-shared=no
vo-amrwbenc:
	$(vo-amrwbenc-dpkg) $(PKG_PATH)/$(vo-amrwbenc-name).$(vo-amrwbenc-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(vo-amrwbenc-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=arm \
		--enable-static \
		--enable-shared \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(vo-amrwbenc-name)

# 不生成.so时添加下面操作
# sed -i 's%SHARED=yes%SHARED=no%' ./Makefile ./librtmp/Makefile && \

rtmp-pthread = -lpthread
ifeq ($(ARCH),aarch64)
rtmp-pthread = -pthread
endif
rtmp:
	$(rtmp-dpkg) $(PKG_PATH)/$(rtmp-name).$(rtmp-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(rtmp-name) && \
	sed -i 's%prefix=/usr/local%prefix=$(LIBS_PATH)%' ./Makefile ./librtmp/Makefile && \
	sed -i 's%CC=\x24(CROSS_COMPILE)gcc%CC=$(GCC)%' ./Makefile ./librtmp/Makefile && \
	sed -i 's%LD=\x24(CROSS_COMPILE)ld%LD=$(LD)%' ./Makefile ./librtmp/Makefile && \
	sed -i 's%AR=\x24(CROSS_COMPILE)ar%AR=$(AR)%' ./Makefile ./librtmp/Makefile && \
	sed -i 's%CFLAGS=%CFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib -ldl %' ./Makefile ./librtmp/Makefile && \
	sed -i 's%LDFLAGS=%LDFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib -ldl %' ./Makefile && \
	sed -i '/^CFLAGS=/a LDFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib\' ./librtmp/Makefile && \
	sed -i 's%-lpthread%$(rtmp-pthread)%' ./Makefile ./librtmp/Makefile && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(rtmp-name)

lame-host = $(ARCH)
ifeq ($(ARCH),aarch64)
lame-host = arm
endif
lame:
	$(lame-dpkg) $(PKG_PATH)/$(lame-name).$(lame-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(lame-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=$(lame-host) \
		--enable-static \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(lame-name)

curl-host = $(ARCH)
ifeq ($(ARCH),aarch64)
curl-host = arm
endif
curl:
	$(curl-dpkg) $(PKG_PATH)/$(curl-name).$(curl-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(curl-name) && \
	export PKG_CONFIG_PATH=$(LIBS_PATH)/lib/pkgconfig && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(curl-host) \
		--disable-dict \
        --disable-gopher \
        --disable-ldap \
		--disable-ldaps \
        --disable-manual \
        --disable-pop3 \
		--disable-smtp \
		--disable-imap \
        --disable-rtsp \
        --disable-smb \
        --disable-telnet \
        --disable-verbose \
		--with-pic \
		--enable-static \
		--with-ssl=$(LIBS_PATH) \
		--with-sysroot=$(SYSROOT) \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		OBJDUMP=$(OBJDUMP) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(curl-name)

openssl:
	$(openssl-dpkg) $(PKG_PATH)/$(openssl-name).$(openssl-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(openssl-name) && \
	./config \
		--prefix=$(LIBS_PATH) \
		$(EXT_INCS) \
		$(EXT_LIBS) \
		shared \
		-no-asm \
		-no-unit-test && \
	sed -i 's%CC= gcc%CC= $(GCC)%' ./Makefile && \
	sed -i 's%AR= ar%AR= $(AR)%' ./Makefile && \
	sed -i 's%/usr/bin/ranlib%$(RANLIB)%' ./Makefile && \
	sed -i 's%NM= nm%NM= $(NM)%' ./Makefile && \
	sed -i 's%MAKEDEPPROG= gcc%MAKEDEPPROG= $(GCC)%' ./Makefile && \
	sed -i 's%CFLAG= %CFLAG= $(EXT_CFLAGS) $(EXT_INCS) -fPIC %' ./Makefile && \
	sed -i 's%-m64% %' ./Makefile && \
	make depend && \
	sed -i 's%CFLAG= %CFLAG= $(EXT_LIBS) -fPIC %' ./Makefile && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(openssl-name)

x264-host = armv7a-linux
ifeq ($(ARCH),x86_64)
x264-host =
endif
x264:
	$(x264-dpkg) $(PKG_PATH)/$(x264-name).$(x264-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(x264-name) && \
	sed -i 's%CC="\x24{CC-\x24{cross_prefix}gcc}"%CC=$(GCC)%' ./configure && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(x264-host) \
		--disable-cli \
		--disable-asm \
		--enable-pic \
		--enable-static \
		--enable-shared \
		--extra-cflags="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	sed -i 's%gcc-ar%$(AR)%' ./config.mak && \
	sed -i 's%gcc-ranlib%$(RANLIB)%' ./config.mak && \
	sed -i 's%Wno-maybe-uninitialized%Wno-uninitialized%' ./config.mak && \
	sed -i 's%^CFLAGS=%CFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)%' ./config.mak && \
	sed -i 's%^LDFLAGS=%LDFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)%' ./config.mak && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(x264-name)

# 生成.so时 --enable-shared
vpx-target = armv7-linux-gcc
vpx-pthread = -lpthread
ifeq ($(ARCH),aarch64)
vpx-target = armv8-linux-gcc
vpx-pthread = -pthread
endif
vpx:
	$(vpx-dpkg) $(PKG_PATH)/$(vpx-name).$(vpx-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(vpx-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--target=$(vpx-target) \
		--disable-webm-io \
		--disable-libyuv \
		--disable-runtime_cpu_detect \
		--disable-neon \
		--disable-neon_asm\
		--disable-tools \
		--disable-examples \
		--disable-debug \
		--disable-debug-libs \
		--disable-install-docs \
		--disable-vp9 \
		--disable-unit-tests \
		--enable-shared \
		--enable-pic \
		--enable-static \
		--enable-install-srcs \
		--enable-install-libs \
		--enable-install-bins && \
	sed -i 's%CC=gcc%CC=$(GCC)%' ./libs-$(vpx-target).mk && \
	sed -i 's%CXX=g++%CXX=$(GXX)%' ./libs-$(vpx-target).mk && \
	sed -i 's%AR=ar%AR=$(AR)%' ./libs-$(vpx-target).mk && \
	sed -i 's%LD=gcc%LD=$(GCC)%' ./libs-$(vpx-target).mk && \
	sed -i 's%AS=as%AS=$(AS)%' ./libs-$(vpx-target).mk && \
	sed -i 's%STRIP=strip%STRIP=$(STRIP)%' ./libs-$(vpx-target).mk && \
	sed -i 's%NM=nm%NM=$(NM)%' ./libs-$(vpx-target).mk && \
	sed -i 's%CFLAGS  =%CFLAGS = $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)%' ./libs-$(vpx-target).mk && \
	sed -i 's%CXXFLAGS  =%CXXFLAGS = $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)%' ./libs-$(vpx-target).mk && \
	sed -i 's%LDFLAGS =%LDFLAGS = $(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)%' ./libs-$(vpx-target).mk && \
	sed -i 's%-lpthread%$(vpx-pthread)%' ./libs-$(vpx-target).mk && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(vpx-name)

freetype-sys-gcc = gcc
ifeq ($(GCC),clang)
freetype-sys-gcc = clang
endif
freetype:
	$(freetype-dpkg) $(PKG_PATH)/$(freetype-name).$(freetype-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(freetype-name) && \
	./configure \
		--prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--with-pic \
		--with-zlib=no \
		--with-harfbuzz=no \
		CC=$(GCC) \
		LD=$(LD) \
		STRIP=$(STRIP) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		CPPFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" \
		LIBPNG_CFLAGS="-I$(LIBS_PATH)/include" \
		LIBPNG_LIBS="-L$(LIBS_PATH)/lib -lpng12" && \
	sed -i 's%gcc%$(freetype-sys-gcc)%' ./builds/unix/unix-cc.mk && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(freetype-name)

# 生成.so时 --enable-shared 反则 --enable-shared=no
png:
	$(png-dpkg) $(PKG_PATH)/$(png-name).$(png-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(png-name) && \
	./configure \
		-prefix=$(LIBS_PATH) \
		--host=$(ARCH) \
		--enable-static \
		--enable-shared \
		--with-pic \
		CC=$(GCC) \
		NM=$(NM) \
		AR=$(AR) \
		AS=$(AS) \
		STRIP=$(STRIP) \
		RANLIB=$(RANLIB) \
		LD=$(LD) \
		CFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -I$(LIBS_PATH)/include -L$(LIBS_PATH)/lib" \
		LDFLAGS="$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS)" && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(png-name)

# 生成.so时删去 --static
zlib:
	$(zlib-dpkg) $(PKG_PATH)/$(zlib-name).$(zlib-tail) $(LIBS_PATH) && \
	cd $(LIBS_PATH)/$(zlib-name) && \
	sed -i '1i\CC=$(GCC)' ./configure && \
	./configure --prefix=$(LIBS_PATH) && \
	sed -i 's%AR=ar%AR=$(AR)%' ./Makefile && \
	sed -i 's%RANLIB=ranlib%RANLIB=$(RANLIB)%' ./Makefile && \
	sed -i 's%CFLAGS=%CFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) -fPIC %' ./Makefile && \
	sed -i 's%SFLAGS=%SFLAGS=$(EXT_CFLAGS) $(EXT_INCS) $(EXT_LIBS) %' ./Makefile && \
	make -j4 && make install && \
	cd - && \
	rm -rf $(LIBS_PATH)/$(zlib-name)
